/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io;

import java.util.Stack;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openaecfm.spf.model.BinaryParameter;
import org.openaecfm.spf.model.ComplexEntityInstance;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.HeaderEntity;
import org.openaecfm.spf.model.IntegerParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.Node;
import org.openaecfm.spf.model.OmittedParameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.SimpleRecord;
import org.openaecfm.spf.model.StringParameter;
import org.openaecfm.spf.model.TypedParameter;

/**
 * @author Qiu Kuining
 */
public abstract class AbstractSPFReadAwareImpl {

    private int numbersOfHe;
    private int numbersOfEi;
    private Stack<Node> stack = new Stack<Node>();
    protected final Log logger = LogFactory.getLog(getClass());

    public void beginOfExchangeFile() {
        logger.info("Begin of Part 21 file");
    }

    public void beginOfHeaderSection() {
        logger.info("Begin of HEADER Section");
    }

    public void newHeaderEntity(HeaderEntity he) {
        stack.push(he);
    }

    public void endOfHeaderEntity(HeaderEntity he) {
        stack.pop();
        numbersOfHe++;
    }

    public void newTypedParameter(TypedParameter tp) {
        stack.peek().addChild(tp);
        stack.push(tp);
    }

    public void newOmittedParameter(OmittedParameter op) {
        stack.peek().addChild(op);
    }

    public void newDollarParameter(DollarParameter dp) {
        stack.peek().addChild(dp);
    }

    public void newIntegerParameter(IntegerParameter ip) {
        stack.peek().addChild(ip);
    }

    public void newRealParameter(RealParameter rp) {
        stack.peek().addChild(rp);
    }

    public void newStringParameter(StringParameter sp) {
        stack.peek().addChild(sp);
    }

    public void newEntityInstanceNameParameter(EntityInstanceNameParameter einp) {
        stack.peek().addChild(einp);
    }

    public void newEnumerationParameter(EnumerationParameter ep) {
        stack.peek().addChild(ep);
    }

    public void newBinaryParameter(BinaryParameter bp) {
        stack.peek().addChild(bp);
    }

    public void newListParameter(ListParameter lp) {
        stack.peek().addChild(lp);
        stack.push(lp);
    }

    public void endOfHeaderSection() {
        logger.info("End of Header Section");
    }

    public void beginOfDataSection() {
        logger.info("Begin of Data Section");
    }

    public void newSimpleEntityInstance(SimpleEntityInstance sei) {
        stack.push(sei);
    }

    public void endOfEntityInstance(EntityInstance ei) {
        stack.pop();
        numbersOfEi++;
    }

    public void newComplexEntityInstance(ComplexEntityInstance cei) {
        stack.push(cei);
    }

    public void newSimpleRecord(SimpleRecord sr) {
        stack.peek().addChild(sr);
        stack.push(sr);
    }

    public void endOfDataSection() {
        logger.info("End of Data Section");
    }

    public void endOfExchangeFile() {
        logger.info("End of Part 21 file");
    }

    public void endOfNode() {
        stack.pop();
    }

    public int sizeOfHeaderEntity() {
        return numbersOfHe;
    }

    public int sizeOfEntityInstance() {
        return numbersOfEi;
    }
}
